package com.akeil.flash.dialog;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.akeil.flash.config.EDeviceType;
import com.akeil.flash.config.IConstantsConfig;
import com.akeil.flash.pojo.Other;
import com.akeil.flash.service.IOtherService;
import com.akeil.flash.service.OtherServiceImpl;

import flash.Application;

public class AdvancedSettingOpenDialog extends TitleAreaDialog {
	IOtherService otherService = Application.applicationContext.getBean(OtherServiceImpl.class);
	DecimalFormat df1 = new DecimalFormat("0.0");
	private Text text40;
	private Text text60;
	private Text text80;
	private Text text100;
	private Text text120;
	private Text text140;
	private Text text160;
	private Text text180;
	private Text text200;
	private Text text220;
	private Text text240;
	private Text text260;
	private Text text280;
	private Text text300;
	private Text text320;
	private Text text340;
	private Text text360;
	private Text text380;
	private Text textPreviousPower;
	private Text textNextPower;
	List<Other> others = new ArrayList<Other>();
	private final String[] NAMES={"40","60","80","100","120","140","160","180","200","220","240","260","280","300","320","340","360","380","P","N"};
	private Text[] CONTROLS = null;
	
	public AdvancedSettingOpenDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(4, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("40");
				
		text40 = new Text(composite_1, SWT.BORDER);
		text40.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
	
		label_1.setText("220");
		text220 = new Text(composite_1, SWT.BORDER);
		text220.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(composite_1, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("60");
		
		text60 = new Text(composite_1, SWT.BORDER);
		text60.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_10 = new Label(composite_1, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_10.setText("240");
		
		text240 = new Text(composite_1, SWT.BORDER);
		text240.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_3 = new Label(composite_1, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("80");
		
		text80 = new Text(composite_1, SWT.BORDER);
		text80.setText("");
		text80.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_11 = new Label(composite_1, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_11.setText("260");
		
		text260 = new Text(composite_1, SWT.BORDER);
		text260.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_4 = new Label(composite_1, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText("100");
		
		text100 = new Text(composite_1, SWT.BORDER);
		text100.setText("");
		text100.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_12 = new Label(composite_1, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_12.setText("280");
		
		text280 = new Text(composite_1, SWT.BORDER);
		text280.setText("");
		text280.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_5 = new Label(composite_1, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setText("120");
		
		text120 = new Text(composite_1, SWT.BORDER);
		text120.setText("");
		text120.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_13 = new Label(composite_1, SWT.NONE);
		label_13.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_13.setText("300");
		
		text300 = new Text(composite_1, SWT.BORDER);
		text300.setText("");
		text300.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_6 = new Label(composite_1, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_6.setText("140");
		
		text140 = new Text(composite_1, SWT.BORDER);
		text140.setText("");
		text140.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_14 = new Label(composite_1, SWT.NONE);
		label_14.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_14.setText("320");
		
		text320 = new Text(composite_1, SWT.BORDER);
		text320.setText("");
		text320.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_7 = new Label(composite_1, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_7.setText("160");
		
		text160 = new Text(composite_1, SWT.BORDER);
		text160.setText("");
		text160.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_15 = new Label(composite_1, SWT.NONE);
		label_15.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_15.setText("340");
		
		text340 = new Text(composite_1, SWT.BORDER);
		text340.setText("");
		text340.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_8 = new Label(composite_1, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setText("180");
		
		text180 = new Text(composite_1, SWT.BORDER);
		text180.setText("");
		text180.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_16 = new Label(composite_1, SWT.NONE);
		label_16.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_16.setText("360");
		
		text360 = new Text(composite_1, SWT.BORDER);
		text360.setText("");
		text360.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_9 = new Label(composite_1, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_9.setText("200");
		
		text200 = new Text(composite_1, SWT.BORDER);
		text200.setText("");
		text200.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_17 = new Label(composite_1, SWT.NONE);
		label_17.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_17.setText("380");
		
		text380 = new Text(composite_1, SWT.BORDER);
		text380.setText("");
		text380.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblA = new Label(composite_1, SWT.NONE);
		lblA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			lblA.setText("A前功率");
		}else{
			lblA.setText("前功率");
		}
		
		textPreviousPower = new Text(composite_1, SWT.BORDER);
		textPreviousPower.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblA_1 = new Label(composite_1, SWT.NONE);
		lblA_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			lblA_1.setText("A后功率");
		}else{
			lblA_1.setText("后功率");
		}
		
		textNextPower = new Text(composite_1, SWT.BORDER);
		textNextPower.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				
		this.setTitle("高级参数");
		CONTROLS = new Text[]{text40,text60,text80,text100,text120,text140,text160,text180,text200,text220,text240,text260,text280,text300,text320,text340,text360,text380,textPreviousPower,textNextPower};
		initData();
		
		return composite;
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("高级参数");
	}
	
	@Override
	public void setErrorMessage(String newErrorMessage) {
		// TODO Auto-generated method stub
		Button okButton = getButton(IDialogConstants.OK_ID);
        if (okButton != null) 
            okButton.setEnabled(newErrorMessage == null);
        
		super.setErrorMessage(newErrorMessage);
	}
	
	private void initData(){
		Other other = null;
		others = otherService.selectAll();
		for(int i = 0;i<NAMES.length;i++){
			if((other = findOtherByName(others,NAMES[i])) != null){
				CONTROLS[i].setText(df1.format(other.getValue()));
			}
		}
	}

	@Override
	protected void okPressed() {
		// TODO 自动生成的方法存根
		saveData();
		super.okPressed();
	}
	
	private void saveData(){
		Other theOther = null;
		for(int i = 0;i<NAMES.length;i++){
			theOther = others.get(i);
			theOther.setValue(Double.parseDouble(CONTROLS[i].getText().trim()));
			otherService.updateById(others.get(i));
		}
	}
	
	private Other findOtherByName(List<Other> others, String name){
		System.out.println("name : " + name);
		for (Other other : others) {
			if(name.equals(other.getName())){
				return other;
			}
		}
		return null;
	}
	
	@Override
	protected Point getInitialSize() {
		// TODO Auto-generated method stub
		return new Point(450,550);
	}
	
}
